package cn.edu.hunau.dao.impl;

import cn.edu.hunau.dao.ProductDao;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.pojo.Product;
import cn.edu.hunau.utils.DBUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 苏佳
 * @Date 2022 12 10 21 41
 **/
public class ProductDaoImpl implements ProductDao {
    @Override
    public String[] queryByPid(Integer Pid) throws DaoException {
        String sql="select path,imagessrc,crumbData,goodsDetails from t_productInfo where pid=?";
        return DBUtils.query(sql,rs -> {
            String[] str=new String[10];
            while (rs.next()){
                str[0]= rs.getString("path");
                str[1] = rs.getString("imagessrc");
                str[2]= rs.getString("crumbData");
                str[3]= rs.getString("goodsDetails");
            }
            return str;
        },Pid);
    }

    @Override
    public Product queryById(Integer id) throws DaoException {
        String sql="select id,pname,price,promoteSales,support,address,evaluateNum,pflag,cid,pimage,pdesc from t_product where id=? and pflag='0'";
        return DBUtils.query(sql,rs -> {
            Product p = null;
            while(rs.next()){
                p=new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("pname"));
                p.setPrice(rs.getDouble("price"));
                p.setPromoteSales(rs.getString("promoteSales"));
                p.setSupport(rs.getString("support"));
                p.setAddress(rs.getString("address"));
                p.setEvaluateNum(rs.getString("evaluateNum"));
                p.setPfalg(rs.getString("pflag"));
                p.setCid(rs.getInt("cid"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
            }
            return p;
        },id);
    }

    @Override
    public int countById(Integer cid) throws DaoException {
        String sql="SELECT COUNT(id) FROM t_product WHERE cid=? AND pflag='0'";
        return DBUtils.query(sql,rs->{
            int count = -1;
            while(rs.next()){
                count = rs.getInt(1);
            }
            return count;
        },cid);
    }

    @Override
    public List<Product> queryByCidLimit(Integer cid, int beginIndex, int size) throws DaoException {
        String sql="SELECT * FROM t_product WHERE cid=? AND pflag='0' LIMIT ?,?";
        return DBUtils.query(sql,rs->{
            List<Product> list = new ArrayList<>();
            while(rs.next()){
                Product p = new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("pname"));
                p.setPrice(rs.getDouble("price"));
                p.setPromoteSales(rs.getString("promoteSales"));
                p.setSupport(rs.getString("support"));
                p.setAddress(rs.getString("address"));
                p.setEvaluateNum(rs.getString("evaluateNum"));
                p.setCid(rs.getInt("cid"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
                list.add(p);
            }
            return list;
        },cid,beginIndex,size);
    }

    @Override
    public int CountByKeyWord(String keyWord) throws DaoException {
        String sql="SELECT COUNT(id) FROM t_product where pname like concat('%',?,'%') AND pflag='0'";
        return DBUtils.query(sql,rs -> {
            int count=-1;
            while(rs.next()){
                count = rs.getInt(1);
            }
            return count;
        },keyWord);
    }

    @Override
    public List<Product> queryByKeyWordLimit(String keyWord, int beginIndex, int size) throws DaoException {
        String sql="SELECT * FROM t_product where pname like concat('%',?,'%') AND pflag='0' LIMIT ?,?";
        return DBUtils.query(sql,rs->{
            List<Product> list = new ArrayList<>();
            while(rs.next()){
                Product p = new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("pname"));
                p.setPrice(rs.getDouble("price"));
                p.setPromoteSales(rs.getString("promoteSales"));
                p.setSupport(rs.getString("support"));
                p.setAddress(rs.getString("address"));
                p.setEvaluateNum(rs.getString("evaluateNum"));
                p.setCid(rs.getInt("cid"));
                p.setPimage(rs.getString("pimage"));
                p.setDesc(rs.getString("pdesc"));
                list.add(p);
            }
            return list;
        },keyWord,beginIndex,size);
    }


}
